var allProject;
var projectPage = 1, projectPages = 0;
var projectCount = 0;

var firstSite = "https://www.atu360.cn/api/phone/findProjectClass?level=0&limit=10";


function loadXMLDoc() {
    loadXMLDoc1();
    loadXMLDoc2();
}

function loadXMLDoc1() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // IE6, IE5 浏览器执行代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var homeMessage = JSON.parse(this.responseText);

            document.getElementById("ADUrlFirst").href = homeMessage.Ad[0].url;
            document.getElementById("ADImageFirst").src = homeMessage.Ad[0].image;
            document.getElementById("ADUrlSecond").href = homeMessage.Ad[1].url;
            document.getElementById("ADImageSecond").src = homeMessage.Ad[1].image;
            document.getElementById("ADUrlThird").href = homeMessage.Ad[2].url;
            document.getElementById("ADImageThird").src = homeMessage.Ad[2].image;
            document.getElementById("ADUrlFour").href = homeMessage.Ad[3].url;
            document.getElementById("ADImageFour").src = homeMessage.Ad[3].image;
            document.getElementById("ADUrlFifth").href = homeMessage.Ad[4].url;
            document.getElementById("ADImageFifth").src = homeMessage.Ad[4].image;
        }
    };
    xmlhttp.open("GET", "https://www.atu360.cn/api/phone/index", true);
    xmlhttp.send();
}

function loadXMLDoc2() {
    var xmlhttp;
    var i = 0;

    if (window.XMLHttpRequest) {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // IE6, IE5 浏览器执行代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            allProject = JSON.parse(this.responseText);

            projectPages = allProject.pages;
            if (projectPage <= projectPages){
            for (; i < allProject.limit; i++ , projectCount++) {
                insertProject(projectCount);
                document.getElementById("newTitle_" + projectCount).innerHTML = allProject.docs[i].titleName;
                document.getElementById("newA_" + projectCount).href = allProject.docs[i].url;
                document.getElementById("newIntroduce_" + projectCount).innerHTML = allProject.docs[i].intro;
                document.getElementById("newImage_" + projectCount).src = allProject.docs[i].icon;
            }
            projectPage += 1;
            }
        }
    };
    xmlhttp.open("GET", firstSite + "&page=" + projectPage, true);
    xmlhttp.send();
}

//创建 media 容器 div
function newMedia(i) {
    var newMedia = document.createElement("div");
    newMedia.id = "newMedia_" + i;
    newMedia.className = "media border-top p-3";
    newMedia.style.marginBottom = "5px";
    document.getElementById("showProject").appendChild(newMedia);
}

//创建 mediabody 容器 div
function newMediaBody(i) {
    var newMediaBody = document.createElement("div");
    newMediaBody.id = "newMediaBody_" + i;
    newMediaBody.className = "meidia-body";
    newMediaBody.style.width = "80%";
    document.getElementById("newMedia_" + i).appendChild(newMediaBody);
}

//创建项目链接
function newA(i) {
    var newA = document.createElement("a");
    newA.id = "newA_" + i;
    newA.href = "#";
    newA.style.color = "black";
    newA.style.textDecoration = "none";
    document.getElementById("newMediaBody_" + i).appendChild(newA);
}

//创建项目标题
function newTitle(i) {
    var newTitle = document.createElement("h4");
    newTitle.id = "newTitle_" + i;
    document.getElementById("newA_" + i).appendChild(newTitle);
}

//创建项目简介
function newIntroduce(i) {
    var newIntroduce = document.createElement("p");
    newIntroduce.id = "newIntroduce_" + i;
    document.getElementById("newMediaBody_" + i).appendChild(newIntroduce);
}

//创建项目 Icon
function newImage(i) {
    var newImage = document.createElement("img");
    newImage.id = "newImage_" + i;
    newImage.href = "#";
    newImage.alt = "Block Graph";
    newImage.className = "ml-3 mt-3";
    newImage.style.width = "200px";
    document.getElementById("newMedia_" + i).appendChild(newImage);
}

//创建新项目
function insertProject(i) {
    newMedia(i);
    newMediaBody(i);
    newA(i);
    newTitle(i);
    newIntroduce(i);
    newImage(i);
}

function newLabelContainer(i) {
    var newLabelContainer = document.createElement("div");
    newLabelContainer.id = "newLabelContainer_" + i;
    document.getElementById("hotLabel").appendChild(newLabelContainer);
}

function newLabel(i) {
    var newLabel = document.createElement("a");
    newLabel.id = "newLabel_" + i;
    newLabel.className = "btn border rounded";
    newLabel.target = "_blank";
    newLabel.href = "#";
    newLabel.color = "black";
    newLabel.style.backgroundColor = "darkgray";
    newLabel.style.marginRight = "10px";
    document.getElementById("newLabelContainer_" + i).appendChild(newLabel);
}